<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class Wxappislogin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $token = $request->header('token');
        $cp_id=$request->header('cpid');
        $memberinfo = Redis::get($token);

        $memberinfo = $memberinfo ? json_decode($memberinfo, true) : '';

        if (!is_array($memberinfo)) {
            return response()->json([
                'code' => 401, 'msg' => '登录失效，请重新登录!'
            ], 200);
        } else {
            if (!isset($memberinfo['id']) || !isset($memberinfo['cp_id']) || intval($memberinfo['id'])<=0 || $cp_id!=$memberinfo['cp_id']) {
                return response()->json([
                    'code' => 401, 'msg' => '登录失效，请重新登录'
                ], 200);
            }
            $res = $next($request);
            return $res;
        }
    }
}
